namespace Microsoft.VisualStudio.EnterpriseTools.OperationDesigner
{
    using Microsoft.VisualStudio.EnterpriseTools.ArtifactModel.Clr;
    using Microsoft.VisualStudio.Modeling;
    using System;
    using System.Windows.Forms;

    public class SummaryColumn : ColumnDescriptor
    {
        public SummaryColumn() : base(ClrBranchStrings.GetString("SummaryColumnTitle"))
        {
        }

        public override void AddEventHandlers(Store store)
        {
            store.EventManagerDirectory.ElementAttributeChanged.Add(store.MetaDataDirectory.FindMetaAttribute(ClrElement.DocSummaryMetaAttributeGuid), new ElementAttributeChangedEventHandler(this.OnSummaryChanged));
        }

        public override bool AllowKeyDownProcessing(KeyEventArgs e)
        {
            if (((e.KeyCode != Keys.Tab) && (e.KeyCode != Keys.F2)) && (e.KeyCode != Keys.Up))
            {
                return (e.KeyCode == Keys.Down);
            }
            return true;
        }

        public override bool AllowKeyPressProcessing(char key, Keys Modifiers)
        {
            return (key == '\t');
        }

        public override object GetEditor(Type editorType)
        {
            return new SummaryEditor();
        }

        public override object GetInPlaceEdit(object component, ref string alternateText)
        {
            object inPlaceEdit = base.GetInPlaceEdit(component, ref alternateText);
            ClrElement element = component as ClrElement;
            if (element != null)
            {
                alternateText = element.DocSummary;
            }
            return inPlaceEdit;
        }

        public override string GetTipText(object component)
        {
            ClrElement element = component as ClrElement;
            if (element != null)
            {
                return element.DocSummary;
            }
            return base.GetTipText(component);
        }

        public override object GetValue(object component)
        {
            ClrElement element = component as ClrElement;
            if (element != null)
            {
                return SummaryHelper.StripValue(element.DocSummary, false);
            }
            return string.Empty;
        }

        public override OperationDesignerValueSupportedStates GetValueSupported(object component)
        {
            bool flag = false;
            ClrElement element = component as ClrElement;
            if ((element != null) && !element.IsRemoved)
            {
                flag = element.Language.CanForwardEngineerComments(element, ClrElement.DocSummaryMetaAttributeGuid);
            }
            if (!flag)
            {
                return OperationDesignerValueSupportedStates.Unsupported;
            }
            return OperationDesignerValueSupportedStates.Supported;
        }

        private void OnSummaryChanged(object sender, ElementAttributeChangedEventArgs e)
        {
            if (base.Host != null)
            {
                base.Host.Refresh(e.ModelElement);
            }
        }

        public override void RemoveEventHandlers(Store store)
        {
            store.EventManagerDirectory.ElementAttributeChanged.Remove(store.MetaDataDirectory.FindMetaAttribute(ClrElement.DocSummaryMetaAttributeGuid), new ElementAttributeChangedEventHandler(this.OnSummaryChanged));
        }

        public override void SetValue(object component, object value)
        {
            ClrElement element = component as ClrElement;
            if (element != null)
            {
                using (Transaction transaction = element.Store.TransactionManager.BeginTransaction(ClrBranchStrings.GetString("ChangeSummaryUndoText")))
                {
                    element.DocSummary = (string) value;
                    transaction.Commit();
                }
            }
        }

        public override string Category
        {
            get
            {
                return ClrBranchStrings.GetString("ImplementationCategory");
            }
        }

        public override string Description
        {
            get
            {
                return ClrBranchStrings.GetString("SummaryPropertyDescription");
            }
        }
    }
}

